/*
 *    This file is part of CeluMap.
 *
 *    CeluMap is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CeluMap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Copyright 2006, 2007, 2008 Gonzalo Merayo
 *    gonzalo (at) celumap.com.ar
 *    www.celumap.com.ar
 *
 */

package map.file;

import map.Map;
import map.MemMap;
import figures.FigureIterator;
import file.FileGetter;
import geometry.Rectangle;

public class CachedFileMap implements Map{

	protected MemMap mm = null;
	protected FileGetter g = null;
	protected String n = null;
	protected Rectangle myArea = null;
	
	public CachedFileMap(FileGetter getter,String name, Rectangle b){
		g = getter;
		n = name;
		myArea = b;
	}
	
	protected void load(){
		FileMap m = new FileMap(g,n);
		mm = new MemMap(m.nelems);
		mm.cacheMap(m);
	}

	public FigureIterator getFigures(Rectangle area) {		
		if(myArea!=null && !myArea.colides(area)){
			if(mm!=null){
				mm = null;
				System.gc();
			}
			return null;
		}	
		if(mm==null)
			load();
		return mm.getFigures(area);
	}
}

	
